home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Clean 1.2.4 / Small Demos / e.icl < prev    next >
Encoding:
Text File  |  1995-03-10  |  982 b   |  46 lines  |  [TEXT/3PRM]

  1. module e
  2.  
  3. /*
  4. Approximation of the number e.
  5.  
  6. Result: A list containing the first NrDigits digits of e = [2,7,1,8,2,8,1,8,2,8,...].
  7. */
  8.  
  9. import StdEnv
  10.  
  11. NrDigits :== 200    // The number of digits of the approximation of e
  12.  
  13. //    Approximating e:
  14.  
  15. Approx_e::[Int]
  16. Approx_e =    [2:Expan ones] where ones= [1:ones]
  17.  
  18. //    Expan expects an infinite list of ones and returns an infinite
  19. //    list containing the digits of the fraction of e ([7,1,8,2,8,...]).
  20.  
  21. Expan::[Int] -> [Int]
  22. Expan f    = [hd ten:Expan    (tl ten)]
  23.     where 
  24.         ten = Ten 2 f
  25.  
  26.         Ten::Int [Int] -> [Int]
  27.         Ten c [p:q]    | Safe k c    =     [k / c,  k mod c  + a1 : b1]
  28.                                 =     [(k + a1) / c, (k + a1) mod c : b1]
  29.         where 
  30.             a1    =    hd ten
  31.             b1    =    tl ten
  32.             ten    =    Ten (c+1) q
  33.             k    =    10 * p
  34.  
  35. Safe::Int Int -> Bool
  36. Safe k c =   k/c  ==  (k + 9)/c 
  37.  
  38. /*
  39. The Start rule    returns the first NrDigits elements of the
  40.                 list of digits returned by the function
  41.                 'Approx_e' by means of the function take.
  42. */
  43.     
  44. Start::[Int]
  45. Start = take NrDigits Approx_e
  46.